var RepairBalance = function () {
  var template = /* html */ `
    <div class="repair-balance" v-loading.fullscreen.lock="loading">
      <div class="repair-balance-btns">
        <el-button v-if="balanceList.length>0" size="medium" type="primary" @click="toBalance">结算</el-button>
      </div>
      <div class="repair-balance-table-div">
        <el-table fit stripe border :data="balanceList" style="width: 100%" height="parent" empty-text="无维修价目">
          <el-table-column label="物料名称" prop="materialName" min-width="120px" header-align="center"></el-table-column>
          <el-table-column label="单价(元)" prop="price" min-width="100px" align="center"></el-table-column>
          <el-table-column label="数量" prop="total" min-width="100px" align="center"></el-table-column>
          <el-table-column label="金额" prop="totalMoney" min-width="120px" align="center"></el-table-column>
          <el-table-column label="实际收款(元)" width="150px" header-align="center">
            <template slot-scope="scope">
              <el-input-number is-decimal-zero style="width:140px" v-model="scope.row.balancePrice" size="mini" :controls="false" :min="0" :precision="2">
              </el-input-number>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  `;

  return {
    props: {
      repair: {
        type: Object,
        required: true
      }
    },

    data: function () {
      return {
        loading: false,
        balanceList: []
      };
    },

    created: function () {
      this.setBalanceList();
    },

    methods: {
      setBalanceList: function () {
        this.loading = true;
        var self = this;
        ajaxRequest('workOrder/getWorkRepairPriceList', {
          type: 1,
          status: 1,
          linkId: this.repair.id
        }).then(function (balanceList) {
          for (var i = 0; i < balanceList.length; i++) {
            var balance = balanceList[i];

            var price = balance.price;
            price = Number(price);
            balance.price = price;

            var totalMoney = price * balance.total;
            totalMoney = _.round(totalMoney, 2);
            balance.totalMoney = totalMoney;

            var balancePrice = balance.balancePrice;
            if (SysUtil.isEmpty(balancePrice)) {
              balancePrice = undefined;
            } else {
              balancePrice = Number(balancePrice);
            }
            balance.balancePrice = balancePrice;
          }
          self.balanceList = balanceList;
          self.loading = false;
        }).catch(function () {
          self.loading = false;
        });
      },

      toBalance:function(){
        var balanceList = this.balanceList;
        var ids = [];
        var balancePrices = [];
        for (var i = 0; i < balanceList.length; i++) {
          var balance = balanceList[i];
          var balancePrice = balance.balancePrice;
          if (SysUtil.isEmpty(balancePrice)) {
            this.$alert('请输入实际收款金额','信息');
            return;
          }

          ids.push(balance.id);
          balancePrices.push(balance.balancePrice);
        }

        this.loading = true;
        var self = this;
        ajaxRequest('workOrder/updateBalancePrice',{
          id:ids.join(','),
          balancePrice:balancePrices.join(',')
        }).then(function(){
          self.loading = false;
          self.$alert('该工单成功结算','信息',{
            callback:function(){
              self.$emit('balance-success');
            }
          });
        }).catch(function(){
          self.loading = false;
        });
      }
    },

    template: template
  };
}();
